﻿Imports System.Windows.Forms
Imports PkgExec
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper



Public Class PkgExecDlgUI

    Dim SSisPkg As New SSISPackage
    Public _MetaData As Microsoft.SqlServer.Dts.Pipeline.Wrapper.IDTSComponentMetaData100
    Dim virtinp As IDTSVirtualInput100 ' = Collection.GetVirtualInput()

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub VarMappingView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles VarMappingView.CellContentClick

    End Sub
    Sub Populate(ByRef dtsComponentMetadata As Microsoft.SqlServer.Dts.Pipeline.Wrapper.IDTSComponentMetaData100)


        Dim Collection As IDTSInput100 = dtsComponentMetadata.InputCollection(0)
        virtinp = Collection.GetVirtualInput()
        Dim VirtInpColumns As IDTSVirtualInputColumnCollection100 = virtinp.VirtualInputColumnCollection

        Dim inputColumns As IDTSInputColumnCollection100 = Collection.InputColumnCollection



        For Each VirtInpColumn As IDTSVirtualInputColumn100 In VirtInpColumns
            
            Dim row As Integer
            If (VirtInpColumn.UsageType = DTSUsageType.UT_READONLY Or VirtInpColumn.UsageType = DTSUsageType.UT_READWRITE) Then
                Dim RowData As Object() = {VirtInpColumn.Name}
                row = VarMappingView.Rows.Add(RowData)

            Else
                Dim RowData As Object() = {VirtInpColumn.Name}
                row = VarMappingView.Rows.Add(RowData)
            End If
            VarMappingView.Rows(row).Tag = CType(VirtInpColumn, Object)
        Next
    End Sub

    Sub PopulateGridCombo()
        For x As Integer = 1 To VarMappingView.Rows.Count
            Dim VarNameComb As New DataGridViewComboBoxCell()
            VarNameComb.Items.Add("<Not Mapped>")
            VarNameComb.Items.Add("<PackageReturnId>")
            For Each VarOn As DTSVariable In SSisPkg.varList
                VarNameComb.Items.Add(VarOn.ObjectName)
            Next
            VarNameComb.Value = "<Not Mapped>"
            VarMappingView.Rows(x - 1).Cells(1) = VarNameComb
        Next
    End Sub
    Private Sub FindFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindFile.Click
        Try
            Dim openFileDialog1 As New OpenFileDialog()
            openFileDialog1.InitialDirectory = "c:\"
            If (openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then

                SSisPkg.File = openFileDialog1.FileName
                Me.FileName.Text = SSisPkg.File
                SSisPkg.GetVarList()
                PopulateGridCombo()

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Function GetColumnMapping() As String
        Dim RetStr As String = ""
        For x As Integer = 1 To VarMappingView.Rows.Count
            Dim MappedColumn As String = VarMappingView.Rows(x - 1).Cells(1).Value
            Dim InputColumn As String = VarMappingView.Rows(x - 1).Cells(0).Value
            Dim VirtInpCol As IDTSVirtualInputColumn100 = CType(VarMappingView.Rows(x - 1).Tag, IDTSVirtualInputColumn100)
            If (MappedColumn <> "<Not Mapped>") Then
                RetStr += InputColumn + ":" + MappedColumn + "|"
                virtinp.SetUsageType(VirtInpCol.LineageID, DTSUsageType.UT_READONLY)
            Else
                virtinp.SetUsageType(VirtInpCol.LineageID, DTSUsageType.UT_IGNORED)
                '   MsgBox(RetStr)
            End If
        Next
        Return RetStr
    End Function


End Class
